/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bs.global.dao;

import bs.global.modelo.Comprobante;
import bs.global.modelo.ComprobantePK;
import javax.ejb.Stateless;


/**
 *
 * @author marceloagustini
 */

@Stateless
public class ComprobanteDAO extends BaseDAO {

    public Comprobante getComprobante(ComprobantePK idPK){

        return em.find(Comprobante.class, idPK);
    }

    public Integer getUltimoNumeroComprobante(String modfor, String codfor){

        try {
            String query = "SELECT Max(PDRMVH_NROFOR) "
                    + "FROM pdrmvh "
                    + "WHERE PDRMVH_MODFOR = '" + modfor + "' "
                    + "AND PDRMVH_CODFOR = '" + codfor + "'";

            Integer ultNumero = (Integer) em.createNativeQuery(query).getSingleResult();

//            System.out.println("ultimo numero obtenido: " + ultNumero);

            if(ultNumero==null) return 0;
            else return ultNumero;

        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Error al consultar ultimo número: " + e.getMessage());
            return -1;
        }

    }

}
